package Top.q3747051.CQPlugin;


import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


public class CQPlugin extends JavaPlugin implements Listener {
    String msg;
    Socket socket = null;
    @Override
    public void onEnable() {
        super.onEnable();
        getLogger().info("QCPlugin Start");
        Bukkit.getPluginManager().registerEvents(this,this);
        NewTherad newTherad =new NewTherad();
        newTherad.start();
    }

    public void SendMessage(String msg) {
        if (socket!=null){
            try {

                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
                PrintWriter pw = new PrintWriter(osw, true);
                pw.write(msg);
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDisable() {
        super.onDisable();
        getLogger().info("QCPlugin Stop");
    }

    @EventHandler
    public void onPlayerSay(AsyncPlayerChatEvent event){
        msg = "["+event.getPlayer().getName() + "]>>>"+event.getMessage();
        getLogger().info(msg);
        SendMessage(msg);
    }
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event){
        msg = event.getPlayer().getName() +"离开了游戏!";
        getLogger().info(msg);
        SendMessage(msg);
    }
    @EventHandler
    public void onPlyaerJoin(PlayerJoinEvent event){
        msg = event.getPlayer().getName() + "上线了!";
        getLogger().info(msg);
        SendMessage(msg);
    }
}